#include<iostream>
#include<vector>
#include<queue>
#include<malloc.h>

void multigrid_one(double *f,int RO,int IO,double *au,double RA,int ncycle_M,int n,double *ig,int cycletype,double *y)
//f是右侧函数对应的向量,n是区间个数加一,ncycle_M是最大迭代次数,RA是相对精确度,RO是限制算子的类型,IO是插值算子的类型,au是程序计算出的最后的结果,ig是初始猜测,cycletype是循环类型.

{
  void V_cycle_one(double *f,double *u, int n, int ng,int ncycle_M ,double RA,int RO,int IO,double *au,double *y);
  void FMG_cycle_one(double *f,double RA,double *u,int ng,int n,int RO,double *au,int IO,int ncycle_M,double *y);
 

 unsigned int ng=0,nN,i;
 double *u,h,h2i,a=-1;

u=(double *)malloc(sizeof(double)*2000);
 h=1.0/(n-1);
 h2i=1.0/(h*h);
 nN=n;
while (nN >>= 1) ng++;
if (n != 1+(1L << ng)) perror("n-1 must be a power of 2 in mglin.");

// std::cout<<ng<<std::endl;
//计算残差值
 for (i=2;i<n;i++){
   u[i]=-h2i*(ig[i-1]+ig[i+1]-2*ig[i])+f[i];
 }
 u[1]=-h2i*(ig[2]-2*ig[1])+f[1];
 u[n]=-h2i*(ig[n-1]-2*ig[n])+f[n];
 

 //判断循环类型
 if (cycletype==0)
   V_cycle_one(f,u,n,ng,ncycle_M,RA,RO,IO,au,y);
 else
   FMG_cycle_one(f,RA,u,ng,n,RO,au,IO,ncycle_M,y);
     

}



 
   
   
   
